/**
 * 
 */
package cardbs.dao;

import cardbs.domain.Department;
import cardbs.domain.Deptype;
import cardbs.domain.Employee;
import cardbs.domain.Located;
import cardbs.domain.ServicePoint;
import cardbs.domain.Work;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.easymock.internal.matchers.Compare;

import cardbs.domain.Part;

/** 
 * <!-- begin-UML-doc -->
 * <!-- end-UML-doc -->
 * @author marianna
 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class LocatedAtDAO extends GenericDAO<Located> {
	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param SP
	 * @return
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public Set<Part> findPartsInSP(ServicePoint SP) {
			// begin-user-code
		// TODO Auto-generated method stub
		int spCode = SP.getServicePointCode();
		Set<Part> partsInSP = new HashSet<Part>();
		
		EntityManager em = JPAUtil.getCurrentEntityManager();
		
		Query query = em.createQuery("select located from Located located where spCode=:insertedspCode");
	    query.setParameter("insertedspCode", spCode);
	    List <Located> results = query.getResultList();
		
	    int size = results.size();
	    if (size>0){
	     Iterator itr = results.iterator();
	     while(itr.hasNext()){
	    	 Located located = (Located) itr.next();
			 Part part = located.getPart();
			 partsInSP.add(part);
	    	}
			return partsInSP;
	    }
	    return null;
		// end-user-code
	}
	public Boolean findPartsByPartCode(int partcode){
		// begin-user-code
		// TODO Auto-generated method stub
		EntityManager em = JPAUtil.getCurrentEntityManager();
		Query query = em.createQuery("select located from Located located where partCode =:insertedPartCode");
		query.setParameter("insertedPartCode", partcode);
	    List results = query.getResultList();
	    Set services = new HashSet(results);
		int size = results.size();
	   
	    if (size!=0){
	    	return true;
	    }
		return false;
		// end-user
	}
	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param part
	 * @param SP
	 * @return
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public Integer getAmount(Part part, ServicePoint SP) {
		// begin-user-code
		// TODO Auto-generated method stub
		int spCode = SP.getServicePointCode();
		int partCode = part.getPartCode();
				
		EntityManager em = JPAUtil.getCurrentEntityManager();
		//EntityManager em = DBUtilities.getEntityManager();
		Query query = em.createQuery("select located from Located located where spCode=:insertedspCode and partCode=:insertedpartCode");
	    query.setParameter("insertedspCode", spCode);
	    query.setParameter("insertedpartCode", partCode);
	    List <Located> results = query.getResultList();
		
	    int size = results.size();
	    if (size>0){
	    	Iterator itr = results.iterator();
	    	Located l = (Located)itr.next();
	    	return l.getStock(); 
	    }
	    return 0;
		// end-user-code
	}

	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param SP
	 * @param newStock
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public Located find(int partCode, int spCode) {
		// begin-user-code
		// TODO Auto-generated method stub
		EntityManager em = JPAUtil.getCurrentEntityManager();
		//EntityManager em = DBUtilities.getEntityManager();
		Query query = em.createQuery("select located from Located located where spCode=:insertedspCode and partCode=:insertedpartCode");
	    query.setParameter("insertedspCode", spCode);
	    query.setParameter("insertedpartCode", partCode);
	    List <Located> results = query.getResultList();
		
	    int size = results.size();
	    if (size>0){
	    	
	    	return results.get(0); 
	    }
	    return null;
		// end-user-code
	}
}